package com.hgx.common.rest.response;

import com.hgx.common.constant.BaseConstants;

import java.io.Serializable;

/**
 * Rest接口统一返回对象
 */
@SuppressWarnings("unused")
public class CommonResponse<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * code
     */
    private String code;

    /**
     * 消息内容
     */
    private String msg;

    /**
     * 返回数据
     */
    private T data;

    public CommonResponse(String code) {
        this.code = code;
    }

    public CommonResponse(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public CommonResponse(String code, T data) {
        this.code = code;
        this.data = data;
    }

    public CommonResponse(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }


    public CommonResponse(BaseConstants baseConstants) {
        this.code = baseConstants.getCode();
        this.msg = baseConstants.getDesc();
    }

    public CommonResponse(BaseConstants baseConstants, String message) {
        this.code = baseConstants.getCode();
        this.msg = message;
    }

    public CommonResponse(BaseConstants baseConstants, String message, T data) {
        this.code = baseConstants.getCode();
        this.msg = message;
        this.data = data;
    }

    public CommonResponse(BaseConstants baseConstants, T data) {
        this.code = baseConstants.getCode();
        this.msg = baseConstants.getDesc();
        this.data = data;
    }


    public CommonResponse() {

    }

    public static <T> CommonResponse<T> ok() {
        return new CommonResponse<>(BaseConstants.SUCCESS);
    }

    public static <T> CommonResponse<T> okMessage(String message) {
        return new CommonResponse<>(BaseConstants.SUCCESS, message);
    }

    public static <T> CommonResponse<T> ok(T data) {
        return new CommonResponse<>(BaseConstants.SUCCESS, data);
    }

    public static <T> CommonResponse<T> ok(String message, T data) {
        return new CommonResponse<>(BaseConstants.SUCCESS, message, data);
    }

    public static <T> CommonResponse<T> error() {
        return new CommonResponse<>(BaseConstants.ERROR);
    }

    public static <T> CommonResponse<T> errorMessage(String message) {
        return new CommonResponse<>(BaseConstants.ERROR, message);
    }

    public static <T> CommonResponse<T> error(String message, T data) {
        return new CommonResponse<>(BaseConstants.ERROR, message, data);
    }

    public static <T> CommonResponse<T> error(String code, String message, T data) {
        return new CommonResponse<>(code, message, data);
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "CommonResponse{" +
                "code='" + code + '\'' +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
